﻿using System;
using System.Collections.Generic;
using System.Text;
using ScriptDumper.Manager;
using Microsoft.Office.Interop.Word;

namespace ScriptsGenerator.Helper
{
    public class DocumentHelper
    {
        /// <summary>
        /// 取得文件中的表格資訊，未轉換過的
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static Dictionary<string, TableProfile> GetTableProfiles(Document document)
        {
            Dictionary<string, TableProfile> tableSchemas = new Dictionary<string, TableProfile>();

            for (int i = 1; i < document.Tables.Count; i++)
            {
                Table table = document.Tables[i];
                Table nextTable = document.Tables[i + 1];

                try
                {
                    // 此表格與下一個表格符合條件才處理; 否則跳過
                    if (table.Rows[1].Cells[1].Range.Text.Contains("Table Profile") &&
                        nextTable.Rows[1].Cells[1].Range.Text.Contains("PK"))
                    {
                        TableProfile tblProfile = new TableProfile();

                        tblProfile.TableName = table.Rows[2].Cells[2].Range.Text.TrimEnd('\a', '\r');
                        tblProfile.UserModule = table.Rows[3].Cells[2].Range.Text.TrimEnd('\a', '\r');
                        tblProfile.ContainsLog = "UPT".Equals(table.Rows[4].Cells[2].Range.Text.TrimEnd('\a', '\r'));
                        tblProfile.ProfileTable = table;
                        tblProfile.SchemaTable = nextTable;
                        tableSchemas.Add(tblProfile.TableName, tblProfile);
                    }
                }
                catch (Exception ex)
                {

                }
            }

            return tableSchemas;
        }

        public static Dictionary<string, DbTable> GetTableInfo(Document WordDocument)
        {
            Dictionary<string, DbTable> tables = new Dictionary<string, DbTable>();

            for (int i = 1; i < WordDocument.Tables.Count; i++)
            {
                Table table = WordDocument.Tables[i];           // Table Profile
                Table nextTable = WordDocument.Tables[i + 1];   // Table Columns

                try
                {
                    // 此表格與下一個表格符合條件才處理; 否則跳過
                    if (table.Rows[1].Cells[1].Range.Text.Contains("Table Profile") &&
                        nextTable.Rows[1].Cells[1].Range.Text.Contains("PK"))
                    {
                        DbTable dbTable = new DbTable();

                        // 取得表格基本資料
                        dbTable.TableName = table.Rows[2].Cells[2].Range.Text.TrimEnd('\a', '\r');
                        dbTable.UsedModule = table.Rows[3].Cells[2].Range.Text.TrimEnd('\a', '\r');
                        dbTable.ContainsLog = "UPT".Equals(table.Rows[4].Cells[2].Range.Text.TrimEnd('\a', '\r'));
                        dbTable.Description = table.Rows[7].Cells[2].Range.Text.TrimEnd('\a', '\r');
                        tables[dbTable.TableName] = dbTable;

                        // 取得表格欄位
                        dbTable.DbColumns = new Dictionary<string, DbColumn>();

                        for (int j = 2; j <= nextTable.Rows.Count; j++)
                        {
                            Row row = nextTable.Rows[j];

                            DbColumn column = new DbColumn();

                            try
                            {
                                string pk = row.Cells[1].Range.Text.TrimEnd('\a', '\r');
                                string name = row.Cells[2].Range.Text.TrimEnd('\a', '\r');
                                string columnType = row.Cells[3].Range.Text.TrimEnd('\a', '\r');
                                string length = row.Cells[4].Range.Text.TrimEnd('\a', '\r');
                                string required = row.Cells[5].Range.Text.TrimEnd('\a', '\r');
                                string defaultValue = row.Cells[6].Range.Text.TrimEnd('\a', '\r');

                                if (!pk.Contains("-"))
                                {
                                    if (!string.IsNullOrEmpty(pk))
                                    {
                                        column.PkSequence = Convert.ToInt16(pk);
                                    }
                                }
                                column.Name = name;
                                column.ColumnType = columnType;

                                if (string.IsNullOrEmpty(column.ColumnType))
                                    continue;

                                if (!string.IsNullOrEmpty(length))
                                {
                                    if ("N".Equals(column.ColumnType))
                                    {
                                        string text = Convert.ToString(length);

                                        if (text.Contains(","))
                                        {
                                            text = text.Replace(")", "").Replace("(", "");

                                            int maxLength = Convert.ToInt16(text.Split(',')[0]);
                                            int minLength = Convert.ToInt16(text.Split(',')[1]);
                                            column.MaxLength = maxLength;
                                            column.MinLength = minLength;
                                        }
                                        else
                                        {
                                            column.MaxLength = Convert.ToInt16(text);
                                        }
                                    }
                                    else if ("TX".Equals(column.ColumnType))
                                    {

                                    }
                                    else
                                    {
                                        column.MaxLength = Convert.ToInt16(length);
                                    }
                                }

                                column.Required = required == "N" ? true : false;

                                if ("N".Equals(column.ColumnType))
                                {
                                    column.DefaultValue = "0";
                                }
                                else
                                {
                                    column.DefaultValue = defaultValue;
                                }
                            }
                            catch (Exception ex)
                            {
                                column.ErrorMessage = ex.Message;
                            }

                            dbTable.DbColumns[column.Name] = column;
                        }

                        dbTable.Columns = new DbColumn[dbTable.DbColumns.Count];
                        dbTable.DbColumns.Values.CopyTo(dbTable.Columns, 0);
                    }
                }
                catch (Exception ex)
                {

                }
            }

            return tables;
        }


    }
}
